/*=============================================================================
** Copyright (c) 2004~ BEA Systems, Inc.
** All rights reserved. 
**
** File Name   : host2eai.c
** Description : PGM for calling from HOST to EAI. (HOST -> EAI)
** History     : 2009.12.xx - Created by jtlee
**               
**
**===========================================================================*/

/*-----------------------------------------------------------------------------
** INCLUDE file             
**---------------------------------------------------------------------------*/
#include <stdio.h>
#include <ctype.h>
#include <atmi.h>
#include <userlog.h>

#include "../include/hostmacro.h"
#include "../include/hostglobal.h"

#define  _DBGLEVEL	100                /* Debug Level */

/*-----------------------------------------------------------------------------
** define & declare global variable
**---------------------------------------------------------------------------*/

static char *rcvbuf;
long  rcvlen;
MsgType		ifmsg;

long	ret_errno;
char log_msg[1024];
/*-----------------------------------------------------------------------------
** Function    : HOST2EAI
** Description : Service Main Routine 
** Buffer Type : CARRAY
** Return      : N/A               
** Args        : msg - Data structure recieved from client  
**---------------------------------------------------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
void
#if defined(__STDC__) || defined(__cplusplus)
HOST2EAI(TPSVCINFO *msg)
#else
HOST2EAI(msg)
TPSVCINFO *msg;
#endif
{
/*
	APPDBG(50 , ("Service Start...\n"));
*/

	/* 
	** allocate recieve buffer 
	*/
	if (allocateBuffer() == FAIL) {
		sprintf( log_msg, "Allocate Buffer Fail");
		dmp(log_msg, 2);
		tpreturn(TPFAIL, 0, NULL, 1, 0);
	}

	/* 
	** call EAI HUB program
	*/
	if (callEAI(msg->name, msg->data, msg->len) == FAIL) {
		tpreturn(TPFAIL, ret_errno, rcvbuf, rcvlen, 0);
	}
	/*
	APPDBG(50 , ("Service End...\n"));
	*/

	/* 
	** send to client
	*/
	if (rcvlen == 0) rcvlen++;
	tpreturn(TPSUCCESS, 0, rcvbuf, rcvlen, 0);
}

/*-----------------------------------------------------------------------------
** Function    : allocateBuffer()
** Description : allocate recieve buffer
** Return      : SUCSSES or FAIL
** Args        : N/A 
**---------------------------------------------------------------------------*/
int 
allocateBuffer()
{
	if((rcvbuf = (char *) tpalloc(BUFFTYPE, NULL, BUFFLEN+1)) == NULL) {
		(void) fprintf(stderr,"Error allocating receive buffer\n");
		return (FAIL);
	}
}

/*-----------------------------------------------------------------------------
** Function    : callEAI()
** Description : call HOST program
** Return      : SUCSSES or FAIL
** Args        : N/A 
**---------------------------------------------------------------------------*/
int
callEAI(svcnm, data, len)
char *svcnm;
char *data;
long len;
{
	int  ret;
	char TranName[XATMI_SERVICE_NAME_LENGTH+1];
	char	tmp_str [1024 * 4];
	char log_msg[1024];

	rcvlen=1;
	ret_errno = 0;
	 
	/* 
	** call EAI HUB program
	*/
	/* 2010.02.26 if logic add(HUBAEJB) */
	if (!strncmp(svcnm, "HUBSEJB", 8)) {
		sprintf(TranName, "%s", "H2ESync");
	} else if (!strncmp(svcnm, "HUBAEJB", 8)) {
		sprintf(TranName, "%s", "H2EAsync");
	} else {
		sprintf( log_msg, "Call EJB Name Fail");
		dmp(log_msg, 2);
		return (FAIL);
	}

	dmp(TranName, 1);

	/* 
	APPDBG(0 , ("Service Name [%s]\n", TranName));
	HexLog(data, len);

	Ebcdic2AsciiStr( data, tmp_str, len );
	HexLog(tmp_str, len);
	*/

	dmp(data, 8, len);

	ret = tpcall(TranName, (char *)data, len, (char **)&rcvbuf, &rcvlen, (long)0);
	ret_errno = tperrno;
	if (ret == -1) {
		/* 2010.01.26 */
		sprintf(log_msg, "Call %s Fail:%s", TranName,tpstrerror(ret_errno));
		dmp(log_msg, 2);

		return (FAIL);
	}

	/*
	APPDBG(0 , ("Len [%d]\n", rcvlen));
	*/
	sprintf( log_msg, "receive data length:%d", rcvlen);
	dmp(rcvbuf, 9, rcvlen);

	dmp(log_msg, 2);
  	/*
	HexLog(rcvbuf, rcvlen);
  	*/

	return (SUCCESS);
}
/*============================<PROGRAM END>===================================*/
